package com.fzdy.dataplatform.controller;

import com.alibaba.fastjson.JSON;
import com.fzdy.dataplatform.controller.api.sysLog.LogListApi;
import com.fzdy.dataplatform.core.common.AjaxCallback;
import com.fzdy.dataplatform.core.common.Check;
import com.fzdy.dataplatform.core.common.TimeStampTool;
import com.fzdy.dataplatform.core.controller.BaseController;
import com.fzdy.dataplatform.core.vo.Page;
import com.fzdy.dataplatform.entity.Syslog;
import com.fzdy.dataplatform.entity.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping(value = "/sysmanage/syslog")
public class LogsController extends BaseController {

    @RequestMapping(value = "/list")
    public String findlog(@RequestBody Page<Syslog> page) {
        Syslog syslog = new Syslog();
        String content=null;
        if (!Check.checkStringNull(page.getCrud())){
            content=page.getCrud();
        }
        if (!Check.checkStringNull(page.getModule())){
            if (Check.checkStringNull(content)){
                content=page.getModule();
            }else{
                content+=page.getModule();
            }
        }
        syslog.setTitle(content);
        syslog.setContent(page.getCreatedate());
        if (page.getUname() != null)
            syslog.setUser(new User(page.getUname()));
        syslogService.getPageByExampleLike(syslog,page);
        AjaxCallback ajaxCallback = new AjaxCallback(Businessdbtoapi(page.getList()), page.getTotalRow());
        return JSON.toJSONString(ajaxCallback);
    }

    private List Businessdbtoapi(List<Syslog> list) {
        List<LogListApi> apis = new ArrayList<>();
        for (Syslog syslog : list) {
            LogListApi api=new LogListApi(syslog.getId(),syslog.getTitle(),syslog.getContent(),
                    TimeStampTool.DateToString("yyyy-MM-dd HH:mm:ss",syslog.getCreatedate()));
            if (syslog.getUser()!=null){
                api.setUname(syslog.getUser().getName());
            }
            apis.add(api);
        }
        return apis;
    }
}
